package com.example.planetexploder.module.bookshelf;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.planetexploder.R;
import com.example.planetexploder.bean.maindata.ItemInfo;
import com.example.planetexploder.module.menu.View_MneuEdit;
import com.example.planetexploder.module.setting.View_Settings;
import com.example.planetexploder.bean.otherdata.MenuItem;
import com.example.planetexploder.constants.SortConst;
import com.example.planetexploder.module.menu.Presenter_Menu;
import com.google.android.material.navigation.NavigationView;

import java.util.ArrayList;

/**
 * 功能：实现菜单选项、侧滑栏菜单的创建以及监听
 * @author lxx
 * 修改日期：2019.11.1
 */
public class View_Fragment extends AppCompatActivity {

    private static final String TAG = "View_Fragment";

    private AppBarConfiguration mAppBarConfiguration;

    private Presenter_BookShelf presenterBookShelf;
    private Presenter_Menu presenterMenu;

    private NavigationView navigationView;
    private DrawerLayout drawerLayout;

    private ArrayList<MenuItem> menuArrayList;
    private int tag1;
    private int tag2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bookshelf);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ActionBar actionBar =getSupportActionBar();

        navigationView = findViewById(R.id.nav_view);
        drawerLayout = findViewById(R.id.drawer_layout);

        presenterBookShelf = new Presenter_BookShelf(View_Fragment.this);
        tag1=presenterBookShelf.get_Tag1();
        tag2=presenterBookShelf.get_Tag2();
        presenterMenu = new Presenter_Menu(getApplicationContext());

        if(tag1 == 0&&tag2 ==0) {
            actionBar.setTitle("全部");
        }
        else if(tag1 != 0&&tag2 ==0){
            switch (tag1){//显示当前的自定义标签
                case 1:actionBar.setTitle("未读");break;
                case 2:actionBar.setTitle("在读");break;
                case 3:actionBar.setTitle("已读");break;
                case 4:actionBar.setTitle("无计划");break;
                default:break;
                }
        }
        else if(tag1 == 0&&tag2 == 1){
            actionBar.setTitle("未分类");
        }
        else if(tag1 == 0&&tag2 > 1){
            actionBar.setTitle(presenterBookShelf.getTagName(tag2));
        }

        /**
         * 功能：初始化侧滑栏菜单
         */
        initialMenuItems();

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);

        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_first)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

        /**
         * 功能：监听侧滑菜单的点击情况，并作出相应的处理
         */
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull android.view.MenuItem menuItem) {
                if (menuItem.getGroupId() == 1) {
                    if(menuItem.getItemId() == 1){
                        presenterBookShelf.set_AllTag(0,1);transmitClickSelected();
                    }
                    else{
                        int temp = menuItem.getItemId();
                        presenterBookShelf.set_AllTag(0,menuArrayList.get(temp-2).getId());
                        transmitClickSelected();
                    }
                } else {
                    switch (menuItem.getItemId()) {
                        case R.id.nav_all:
                            presenterBookShelf.set_AllTag(0,0);transmitClickSelected();
                            break;
                        case R.id.nav_weidu:
                            presenterBookShelf.set_AllTag(1,0);transmitClickSelected();
                            break;
                        case R.id.nav_zaidu:
                            presenterBookShelf.set_AllTag(2,0);transmitClickSelected();
                            break;
                        case R.id.nav_yidu:
                            presenterBookShelf.set_AllTag(3,0);transmitClickSelected();
                            break;
                        case R.id.nav_wujihua:
                            presenterBookShelf.set_AllTag(4,0);transmitClickSelected();
                            break;
                        case 99999:
                            if(menuArrayList.size()+1>30){
                               reminderUser(View_Fragment.this);
                            }
                            else{newTagBookShelf(View_Fragment.this);}
                            break;
                        case 99998:
                            Intent intentSetting = new Intent(View_Fragment.this, View_Settings.class);
                            startActivity(intentSetting);
                            break;
                        case 99997:
                            Intent intentMenuEdit = new Intent(View_Fragment.this, View_MneuEdit.class);
                            startActivity(intentMenuEdit);
                            break;
                        default:
                            break;
                    }
                }
                drawerLayout.closeDrawer(GravityCompat.START);//点击菜单后关闭左边菜单
                return true;
            }
        });
    }
    /**
     * 功能：实现侧滑栏菜单的框架
     */
    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

    /**
     * 功能：初始化新的自定义菜单
     */
    public void initialMenuItems() {
        menuArrayList = presenterMenu.create_Menu();
        navigationView.getMenu().add(1, 1, 0, "未分类");
        for (int i = 0; i < menuArrayList.size(); i++) {
            navigationView.getMenu().add(1, i+2, 0, (menuArrayList.get(i).getTitle().length()>10)?menuArrayList.get(i).getTitle().substring(0,11): menuArrayList.get(i).getTitle());
        }
        navigationView.getMenu().add(2, 99999, 0, "新建分类").setIcon(R.drawable.plus);
        navigationView.getMenu().add(2, 99997, 0, "编辑分类").setIcon(R.drawable.setting);
        navigationView.getMenu().add(2, 99998, 0, "设置").setIcon(R.drawable.setting);
    }
    /**
     * 功能：刷新自定义菜单
     */
    public void refreshMenu() {
        menuArrayList = presenterMenu.create_Menu();
        navigationView.getMenu().removeGroup(1);
        navigationView.getMenu().removeGroup(2);
        navigationView.getMenu().removeGroup(3);
        initialMenuItems();
    }

    /**
     * 功能：重写onCreateOptionMenu()方法来添加一个选项菜单
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = new MenuInflater(this);
        menuInflater.inflate(R.menu.menu_bookshlf, menu);
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * 功能：重写onOptionsItemSelected()方法，指定各个菜单被选择时，做出相应处理
     */
    @Override
    public boolean onOptionsItemSelected(@NonNull android.view.MenuItem item) {
        switch (item.getItemId()) {
            case R.id.Updata_BookShelf:
                presenterBookShelf.pSort.set_SortWay(SortConst.ORDER_BY_MOD);
                transmitClickSelected();
                break;

            case R.id.CreateDown_BookShelf:
                presenterBookShelf.pSort.set_SortWay(SortConst.ORDER_BY_CRE_DOWN);
                transmitClickSelected();
                break;

            case R.id.CreateUp_BookShelf:
                presenterBookShelf.pSort.set_SortWay(SortConst.ORDER_BY_CRE_UP);
                transmitClickSelected();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 功能：在View_Content和View_Fragment之间实现信息传递
     */
    public void transmitClickSelected() {
        finish();
        Intent intent = new Intent(this,View_Fragment.class);
        startActivity(intent);
    }

    /**
     * 功能：显示创建自定义标签的对话框
     */
    public void newTagBookShelf(final Context context) {
        final EditText newTag = new EditText(context);
        new AlertDialog.Builder(context).setTitle("新建自定义标签")
                //监听保存键的点击行为
                .setView(newTag).setNegativeButton("取消", null)
                .setPositiveButton("保存", new DialogInterface.OnClickListener() {
                    //保存摘抄集名字
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                            if (newTag.getText().toString().length() != 0) {
                                if(newTag.getText().toString().length()>40) {
                                    Toast.makeText(View_Fragment.this, "您输入的文字字数已到上限:40/40", Toast.LENGTH_SHORT).show();
                                }
                                else{
                                    presenterBookShelf.create_Tag(newTag.getText().toString());
                                    refreshMenu();
                                }
                            } else {
                                Toast.makeText(View_Fragment.this, "您输入文字字数不能为零", Toast.LENGTH_SHORT).show();
                            }
                        }
                }).show();
    }

    /**
     *功能：提示用户自定义标签已到上限
     */
    public  void reminderUser(final Context context){
        new AlertDialog.Builder(context).setView(null).setMessage("您当前自定义标签已到上限：30/30").show();
    }
}
